from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app


class MainHandler(webapp.RequestHandler):
  def get(self):
    name = self.request.get ('name')
    self.response.out.write('Hello '+ name)

class GoodByeHandler (webapp.RequestHandler):
  def get(self):
    self.response.out.write('Goodbye app engine world!')

class DiceHandler (webapp.RequestHandler):
  def get(self):
    times= self.request.get('times')
 
    while not (times.isdigit()):
        self.response.out.write('please type in another time<br>')
        times=self.request.get('times')

    import random
    import types

    # Number of times to roll the dice.
    DICE_ROLLS = 0
    doublesix = False

    NUMSIDES = 6

    def RollDice():
	    return random.randint(1, int(NUMSIDES))

    while doublesix == False:
	    # Simulate rolling two dice.
	    dice_1 = RollDice()
	    dice_2 = RollDice()
	    self.response.out.write ('Rolls: '+ str(dice_1) +' '+ str(dice_2)+ '<br>')

	    DICE_ROLLS= DICE_ROLLS +1 
	    if dice_1 == dice_2:

		    if dice_1 ==1:
			    self.response.out.write ("Got snake eyes!<br>")

		    if dice_1 == 6:
			    self.response.out.write("Got double sixes!<br>")
			    doublesix= True


    self.response.out.write("Rolling the dice " + str(DICE_ROLLS) + " times<br>")

def main():
  application = webapp.WSGIApplication([('/hello.*', MainHandler), ('/goodbye.*', GoodByeHandler), ('/dice.*', DiceHandler)], debug=True)

  run_wsgi_app(application)

if __name__ == '__main__':
  main()
